$(function() {
    for (i=0;i<$("#shortcutCount").val();i++) {
      var obj = $("#dragItem"+i);
      makeDragableItem(obj, "dragName"+i);
      addMenu(obj);
    }
    var name = $("#name"),
      link = $("#link"),
      allFields = $([]).add(link);
    tips = $("#validateTips");

    function updateTips(t) {
      tips.text(t).effect("highlight",{},1500);
    }

    function checkLength(o,n,min,max) {
      if ( o.val().length > max || o.val().length < min ) {
	o.addClass('ui-state-error');
	updateTips("长度必须在: "+min+" 到 "+max+" 之间");
	return false;
      } else {
	return true;
      }

    }

    function checkRegexp(o,regexp,n) {
      if ( !( regexp.test( o.val() ) ) ) {
	o.addClass('ui-state-error');
	updateTips(n);
	return false;
      } else {
	return true;
      }
    }

    function isEmptyJson(obj) {
      for (var i in obj) {
	return false;
      }
      return true;
    }

    function makeDragableItem(obj, name) {
      obj.draggable({
	handle: name,
	    stop: function(){
	    $.ajax({
	      type: "POST",
		  url: "/move",
		  data: "id="+$(this).find('#shortcutID').val()+"&left="+this.style.left+"&top="+this.style.top,
	      });
	  },
	});
    }

    function addMenu(obj) {
      obj.contextMenu({
        menu: 'myMenu'
	    },
        function(action, el, pos) {
	  if (action == 'delete') {
	    $.post("/delete", "id="+$(el).find("#shortcutID").val(),function(data) {
		if (data.result == true) {
		  $(el).remove();
		}
	      }, "json");
	  }
	});
    }
      
    $("#addShortcut").dialog({
      bgiframe: true,
	  width: 600,
	  autoOpen: false,
	  modal: true,
	  buttons: {
	  '添加': function() {
	    var bValid = true;
	    allFields.removeClass('ui-state-error');

	    bValid = bValid && checkLength(link,"link",4,80);
	    
	    if (bValid) {
	      var dataString = {'link': link.val()};
	      $.post("/quickadd", dataString, function(data){
		  if (isEmptyJson(data)){
		    link.addClass('ui-state-error');
		    updateTips('地址无效！');
		  }
		  else {
		    var logoPath = '/images/nologo.png';
		    if (data.logo != 'None') {
		      logoPath = data.logo;
		    }
		    var newShortcutHtml = '<div id="shortcutItem"><div id="dragItem' + data.id + '" style="position:relative;"><div id="shortcutLogo"><a href="' + data.link + '"><img src=' + logoPath +  ' width="64" height="64"/></a></div><div id="shortcutName"><input type="hidden" id="shortcutID" value="' + data.id + '" /><p id="dragName' + data.id + '">' + data.name + '</p></div></div></div>';
		    $('#desktopArea').append(newShortcutHtml);
		    var obj = $("#dragItem"+data.id);
		    makeDragableItem(obj, "dragName"+data.id);
		    addMenu(obj);
		    $('#addShortcut').dialog('close');
		  }
		}, "json");
	    }
	  }
	},
	  close: function() {
	  allFields.val('').removeClass('ui-state-error');
	}
      });
      
    $('#create-shortcut')
      .click(function() {
	  link.val('http://');
	  $('#addShortcut').dialog('open');
	});

    $('#dock2').Fisheye({
      maxWidth: 60,
	  items: 'a',
	  itemsText: 'span',
	  container: '.dock-container2',
	  itemWidth: 40,
	  proximity: 80,
	  alignment : 'left',
	  valign: 'bottom',
	  halign : 'center'
	  }
      );
  });
